import React, {Component} from 'react';
import HeadComponent from "../common/headComponent";
import ContentComponent from "../common/contentComponent";
import commonConfig from "../../static/js/commonConfig";
import '../../static/scss/fileViewer/textViewer.scss'

class TxtViewerApp extends Component {
    state = {
        content: '',
        name: ''
    }

    componentWillMount() {
        let {bucketName, fileUrl, name} = commonConfig.getUrlParams(this);
        this.getFileContent(bucketName, fileUrl);
        name = name.substring(0, name.lastIndexOf('.'));
        this.setState({name})
    }

    getFileContent = (bucketName, fileUrl) => {
        commonConfig.getServiceFileContent({bucketName, fileUrl}, msg => {
            let content = commonConfig.filterInfo(msg);
            this.setState({content})
        });
    }

    render() {
        const {content, name} = this.state;
        return (
            <>
                <div className="pdf_viewer_module text_viewer_module">
                    <HeadComponent noNav={true} {...this.props}>
                        <div className="viewer_head float-left">
                            <h3>在线文件预览</h3>
                        </div>
                    </HeadComponent>
                    <div className="main_container animate__animated animate__fadeIn">
                        <ContentComponent copyrightStyle={{position: 'static'}}>
                            <div>
                                <h4>{name}</h4>
                                <div className="text_content" dangerouslySetInnerHTML={{__html: content}}/>
                            </div>
                        </ContentComponent>
                    </div>
                </div>
            </>
        )
    }
}

export default TxtViewerApp